package com.kikikeji.weizhuo.manager;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;

import com.kikikeji.weizhuo.data.AppInfo;
import com.kikikeji.weizhuo.CellLayout;
import com.kikikeji.weizhuo.Launcher;
import com.kikikeji.weizhuo.data.ShortcutInfo;
import com.kikikeji.weizhuo.Workspace;

import java.util.ArrayList;

/**
 * Created by zhang on 2016/12/3.
 */
public class CustomViewManager extends BaseManager implements ViewManager {
    private CellLayout mCustomView;
    private HideAppManager mHideAppManager;

    private boolean hasPassword;
    private Workspace mWorkspace;

    public CustomViewManager(Launcher mLauncher, HideAppManager hideAppManager) {
        super(mLauncher);
        mHideAppManager = hideAppManager;
    }

    @Override
    public void addView(View view, ViewGroup.LayoutParams layoutParams) {

    }

    @Override
    public void updateViewLayout(View view, ViewGroup.LayoutParams layoutParams) {

    }

    @Override
    public void removeView(View view) {

    }

    public void addHideApps() {

        ArrayList<AppInfo> hideApps = mHideAppManager.getHideApps();

        if (hideApps != null){
            for (AppInfo appInfo : hideApps){
                ShortcutInfo shortcut = new ShortcutInfo(appInfo);
                View child = mLauncher.createShortcut(mCustomView,shortcut);

                ViewGroup.LayoutParams genericLp = child.getLayoutParams();
                CellLayout.LayoutParams lp;
                if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
                    lp = new CellLayout.LayoutParams(1, 1, 1,1);
                } else {
                    lp = (CellLayout.LayoutParams) genericLp;
                    lp.cellX = 1;
                    lp.cellY = 1;
                    lp.cellHSpan = 1;
                    lp.cellVSpan = 1;
                }

                mCustomView.addViewToCellLayout(child,0,-1,lp,true);
            }
        }

    }

    public void addPasswordView() {

    }

    public void doubleClick() {
        if (mWorkspace == null )
            mWorkspace = mLauncher.getWorkspace();

        boolean hasCustomContent = mWorkspace.hasCustomContent();
        if (!hasCustomContent) {
            mCustomView =  mWorkspace.createCustomContentContainer();
            if (hasPassword) {
                addPasswordView();
            } else {
                addHideApps();
            }
        } else {
            mWorkspace.removeCustomContentPage();
        }
    }

    public boolean isHasPassword() {
        return hasPassword;
    }

    public void setHasPassword(boolean hasPassword) {
        this.hasPassword = hasPassword;
    }
}
